$(document).ready(
		
		function() {
			$("#nav-one").dropmenu();
			
			$( "#accordion" ).accordion();
			
			  $(function() {
			  		$( "#dob" ).datepicker({
			  			changeMonth: true,
			  			changeYear: true
			  		});
			  	});
					
			//Display Loading Image
			function Display_Load()
			{
				$("#loading").fadeIn(900,0);
				$("#loading").html("<img src=resources/images/default/bigLoader.gif />");
			}
			//Hide Loading Image
			function Hide_Load()
			{
				$("#loading").fadeOut('slow');
			};
			
			// load wall
			/*$('.profileview').live("click", function() {
				var profileid = $(this).attr("id");
				var dataString = 'refresh=1&profile_id='+profileid;
								
				Display_Load();
				$.ajax({
					type : "POST",
					url : "profile/home",
					data : dataString,
					cache : false,
					success : function(html) {						
						$("#container").html(html);
					}					
				}); 
				Hide_Load();
				
				return false;
			});
			*/
			
		
			
			// load wall
			/*$('.wall').live("click", function() {	
				var dataString = 'refresh=1';
				Display_Load();
				$.ajax({
					type : "POST",
					url : "profile/home",
					data : dataString,
					cache : false,
					success : function(html) {
						$("#mainContent").html(html);
					}
				});
				Hide_Load();
				
				return false;
			});*/
			
			

			// load message
			$('.messages').live("click", function() {
				var dataString = 'refresh=1';
				Display_Load();
				$.ajax({
					type : "POST",
					url : "messages/inbox",
					data : dataString,
					cache : false,
					success : function(html) {					
						$("#mainContent").html(html);
					}
				});
				Hide_Load();
				return false;
			});
			

			// load members
			/*$('.viewMembers').live("click", function() {
				var dataString = 'refresh=0';
				Display_Load();

				$.ajax({
					type : "POST",
					url : "members/list",
					data : dataString,
					cache : false,
					success : function(html) {
						$("#mainContent").html(html);
					}
				});

				Hide_Load();
				

				return false;
			});
			*/
			
			// load friends
			/*$('.viewfriend').live("click", function() {
				var dataString = 'refresh=0';
				Display_Load();

				$.ajax({
					type : "POST",
					url : "relationships/list",
					data : dataString,
					cache : false,
					success : function(html) {
						$("#mainContent").html(html);
					}
				});

				Hide_Load();
				

				return false;
			});
			*/
			
			// View friend requests
			/*$('.friendreq').live("click", function() {
				var dataString = 'refresh=0';
				Display_Load();
			
				$.ajax({
					type : "POST",
					url : "relationships/pending",
					data : dataString,
					cache : false,
					success : function(html) {
						$("#mainContent").html(html);
					}
				});

				Hide_Load();
			
				return false;
			});
			*/
			
			//friend request accepr/reject
			$('.friendreqaccept').live("click", function() {				
				var reqfileid = $(this).attr("id");
				var dataString = 'action=accept&ID='+reqfileid;
				Display_Load();
			
				$.ajax({
					type : "POST",
					url : "relationship",
					data : dataString,
					cache : false,
					success : function(html) {
						
						if (retFind(html, 'result') == 1)
						{
							$("#pendingreq" + reqfileid).slideUp();
							$("#pendingreq" + reqfileid).html('');
						}
					}
				});

				Hide_Load();
				
				return false;
			});
			
			//friend request accepr/reject
			$('.friendreqreject').live("click", function() {
				var reqfileid = $(this).attr("id");
				var dataString = 'action=reject&ID='+reqfileid;
				Display_Load();
			
				$.ajax({
					type : "POST",
					url : "relationship",
					data : dataString,
					cache : false,
					success : function(html) {
						
						if (retFind(html, 'result') == 1)
						{
							$("#pendingreq" + reqfileid).slideUp();
							$("#pendingreq" + reqfileid).html('');
						}
					}
				});

				Hide_Load();
				
				return false;
			});
	
			
			$('.searchsubmit').live("click", function() {
				var searchbox = $(".search").val();
				if (searchbox == '') {
					var dataString = 'refresh=0';
					Display_Load();

					$.ajax({
						type : "POST",
						url : "members/list",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#mainContent").html(html);
						}
					});
					
					Hide_Load();
				} else {
					var dataString = 'searchword=' + searchbox+'&fullsearch=1';
					
					$.ajax({
						type : "POST",
						url : "members/search",
						data : dataString,
						cache : false,
						success : function(html) {					
							$("#membercontent").html(html);
							$(".pagination").html('');
							
						}

					});
				}
				return false;
			});
		

			$(".search").live("keyup", function() {
				var searchbox = $(this).val();
				if (searchbox == '') {
					var dataString = 'refresh=0';
					Display_Load();

					$.ajax({
						type : "POST",
						url : "members/list",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#mainContent").html(html);							
						}
					});
					
					Hide_Load();
				} else {
					var dataString = 'searchword=' + searchbox+'&fullsearch=0';
					$.ajax({
						type : "POST",
						url : "members/search",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#membercontent").html(html);
							$(".pagination").html('');
						}

					});
				}
				return false;

			});
			
			//Friend add
			// load friends
			$('.addfriend').live("click", function() {
				var name = $(this).attr("name");
				var profileid = $(this).attr("id");
				var userid = $(this).attr("uid");
				//alert(name);
				
				Display_Load();
				
				$.alerts.okButton='&nbsp;Add&nbsp;';

				jConfirm('Are you sure you want to add '+name+' as a friend?', 'Confirmation Dialog',
			    function(r) {
					if(r==true)
					{
						var dataString = 'action=create&profileid=' + profileid+'&userid='+userid;
						$.ajax({
							type : "POST",
							url : "relationship",
							data : dataString,
							cache : false,
							success : function(html) {
								var divname = "#"+userid+profileid;								
								$(divname).html(retFind(html, 'result'));
							}
						});
					}
				});

				Hide_Load();
				
				return false;
			});
			
			$('.friendsearchsubmit').live("click", function() {
				var searchbox = $(".friendsearch").val();
				
				if (searchbox == '') {
					var dataString = 'refresh=0';
					Display_Load();

					$.ajax({
						type : "POST",
						url : "relationships/list",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#mainContent").html(html);
						}
					});
					
					Hide_Load();
				} else {
					var dataString = 'searchword=' + searchbox+'&fullsearch=1';
					
					$.ajax({
						type : "POST",
						url : "relationships/search",
						data : dataString,
						cache : false,
						success : function(html) {					
							$("#membercontent").html(html);
							$(".pagination").html('');
							
						}

					});
				}
				return false;
			});	
			
			
			$(".friendsearch").live("keyup", function() {
				var searchbox = $(this).val();
				if (searchbox == '') {
					var dataString = 'refresh=0';
					Display_Load();

					$.ajax({
						type : "POST",
						url : "relationships/list",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#mainContent").html(html);							
						}
					});
					
					Hide_Load();
				} else {
					var dataString = 'searchword=' + searchbox+'&fullsearch=0';
					$.ajax({
						type : "POST",
						url : "relationships/search",
						data : dataString,
						cache : false,
						success : function(html) {
							$("#membercontent").html(html);
							$(".pagination").html('');
						}

					});
				}
				return false;

			});
			
			// delete friendships
			$('.frienddelete').live("click", function() {
				var ID = $(this).attr("id");
				var name = $(this).attr("name");
				var dataString = 'action=delete&ID=' + ID;
				$.alerts.okButton='&nbsp;Ok&nbsp;';							
				
				jConfirm('Are you sure you want to remove connection with '+name+' ?', 'Confirmation Dialog',
						function(r) {
						if(r==true)
						{								
							$.ajax({
								type : "POST",
								url : "relationship",
								data : dataString,
								cache : false,
								success : function(html) {
									
									if (retFind(html, 'result') == 1)
									{										
										$("#"+ID).slideUp();
										$("#"+ID).html('');
									}
								}
							});
						}
				});
				
				return false;
			});
			
			
		// Profile Updates
		
		$('#basicprofileSubmit').live("click", function() {
			
			var emailVal = document.getElementById("email").value;
			var addressValue = document.getElementById("address").value;
			var dobValue = document.getElementById("dob").value;
			var aboutValue = document.getElementById("about").value;
			var profileNameValue = document.getElementById("profilename").value;
			var nameValue = document.getElementById("name").value;
			var genderValue = document.getElementById("gender").value;
									
			var dataString = 'section=basic&name='+nameValue+'&gender='+genderValue+'&email='+emailVal+'&address='+addressValue+'&dob='+dobValue+'&about='+aboutValue+'&profilename='+profileNameValue;
					
			Display_Load();
			$.ajax({
				type : "POST",
				url : "profile/edit",
				data : dataString,
				cache : false,
				success : function(html) {						
					jAlert('Basic profile information successfully saved!');
				}					
			});
						
			Hide_Load();
				
			return false;
		});
		
		
		$('#profileEduEmp').live("click", function() {
			
			var employer = document.getElementById("emp").value;
			var college = document.getElementById("uni").value;
			var highSchool = document.getElementById("scol").value;
												
			var dataString = 'section=education&employer='+employer+'&college='+college+'&highschool='+highSchool;
								
			Display_Load();
			$.ajax({
				type : "POST",
				url : "profile/edit",
				data : dataString,
				cache : false,
				success : function(html) {						
					jAlert('Education profile information successfully saved!');
				}					
			});			
						
			Hide_Load();
				
			return false;
		});
		
		
		$('#PasswordEmp').live("click", function() {			
		
			var newpasswor = document.getElementById("pwd").value;
			var confirm = document.getElementById("confpwd").value;
			
			var dataString = 'section=passwordchange&newpassword='+newpasswor+'&confirmpassword='+confirm;
								
			Display_Load();
			$.ajax({
				type : "POST",
				url : "profile/edit",
				data : dataString,
				cache : false,
				success : function(html) {		
					if (retFind(html, 'result') == 1)
					{	
						jAlert('Password too short');
					}
					else if (retFind(html, 'result') == 2)
					{
						jAlert('Password confirm mismatch');
					}	
					else
						jAlert('New password information successfully saved!');
				}					
			});			
						
			Hide_Load();
				
			return false;
		});

		///////// facebook cals///////
		
		$('.Submitbuttona').live("click", function() {
			alert("FB");
			var dataString = 'action=createprofile&type=facebook';
			$.ajax({
				type : "POST",
				url : "profile/create",
				data : dataString,
				cache : false,
				success : function(html) {						
					alert(html);
				}					
			});			
						
			
				
			return false;
		});
		
		
		$('.notificationbody').live("click", function() {
			
		});
		
		//////////////////////////////


		});



function ajaxFileUpload() {

	$("#photoloading").ajaxStart(function() {
		$(this).show();
	}).ajaxComplete(function() {
		$(this).hide();
	});

	$.ajaxFileUpload({
		url : 'profile/edit',
		secureuri : false,
		fileElementId : 'profile_photo',
		dataType : 'json',
		data : {
			section : 'photo'
		},
		success : function(data, status) {
			if (typeof (data.error) != 'undefined') {
				if (data.error != '') {
					alert(data.error);
				} else {
					alert("Profile photo successfully uploaded");
					$('.thrColAbsHdr').html(
							data.html.replace(/&amp;/g, '&').replace(/&lt;/g,
									'<').replace(/&gt;/g, '>'));
				}
			}
		},
		error : function(data, status, e) {
			alert(e);
		}
	});

	return false;
}

jQuery(function($) {
	$(".friendsearch").Watermark("Search");
	$(".search").Watermark("Search");
});


// ////////////////////////////////////////////////////////
//
//For get string within search pattern
//
//////////////////////////////////////////////////////////

function onTypeChanged()
{
	var dataString = 'action='+document.getElementById("profile_type").value;
	$.ajax({
		type : "POST",
		url : "profile/create",
		data : dataString,
		cache : false,
		success : function(html) {
			$("#mainContent").html(html);
		}					
	});			
}

function retFind(str, searchPattern) {
	var findex;
	var lindex;
	var val;
	var s_tag;
	var e_tag;
	var tag_len;

	s_tag = "<" + searchPattern + ">";
	e_tag = "</" + searchPattern + ">";
	tag_len = s_tag.length;

	findex = str.indexOf(s_tag) + tag_len;
	lindex = str.indexOf(e_tag);

	val = str.substring(findex, lindex);
	return val;
}
